﻿
/*
 * Javasript based on Functions(JBF) 1.03
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * Created by Fooxee. at 2010.03.08  mailTo:hjccjun@163.com
 */

JBF.incIf('JBF.Dom');


/*

#summary JBF.DD 文档.

= JBF.DD =

对象:JBF.DD

文件:core/DD.js

描述:拖拽类.

|| [#Public_Config 配置项] || [#Public_Properties 属性] || [方法] || [#Public_Events 事件] ||

=== Public Config ===
公开配置

|| *项* || *数据类型* || *描述* ||
|| handleElId	|| String	|| 手柄对象ID ||
|| dragElId		|| String	|| 拖动对象ID ||
|| listeners	|| Object	|| 事件,详见 [#Public_Events 事件] ||
|| 完善中		|| --		|| -- ||


=== Public Events ===
公开事件

|| *事件名* || *描述* || *回调参数* ||
|| begin	|| 对象移动前触发		|| this ||
|| end		|| 对象移动结束后触发	|| this ||
|| move		|| 对象移动中触发		|| this ||

*例子*

{{{
...

//HTML
<div id="msg" style="width: 100px; height: 50px; background:#fff">我是消息</div>
<div id="drag" style="width: 200px; height: 200px; background:#fff; border:1px solid blue; position:absolute;">
<div id="handle" style="width: 200px; height: 50px; background:red">
我是拖动手柄
</div>
我是被拖动的对象
</div>

//js

var dd=new JBF.DD({handleElId:'handle',dragElId:'drag',listeners:{
				  

	'begin':function()
	{
		JBF('msg').html('移动前,触发begin事件');	
	},
	'end':function()
	{
		JBF('msg').html('移动结束,触发end事件');	
	},
	'move':function()
	{
		JBF('msg').html('移动中,触发move事件. X:'+this.current.X+' Y:'+this.current.Y);	
	}

}});

...
}}}

*/
JBF.DD = JBF.createClass({
	
	//private
	events:{},
	//private
	fire:function(obj,type)
	{
		var args=[];
		for(var i=2;i<arguments.length;i++)
		{
			args[i-2]=arguments[i];
		}
		var fn=this.events[type];
		fn.apply(obj,args);
	},
	/*
	构造方法,配置参见上
	*/
	init:function(options)
	{
		this.pos={X:options.X||0, Y:options.Y||0, maxX:options.maxX||0, maxY:options.maxY||0, minX:options.minX||0, minY:options.minY||0};
		this.current={X:0,Y:0};
		JBF.each(options.listeners||{},function(v,i,list)
		{
			if(JBF.isString(v))
			{
				var fn=JBF.isFunction(list[v])?list[v]:JBF.emptyFn;
				this.events[v]=fn;
			}
		},this);
		
		this.begin=false;
		try
		{
			this.handle=JBF(options.handleElId);
			this.drag=JBF(options.dragElId);
		}catch(e){JBF.debug(e);}
		
		this.handle.on('mousedown',function(e,caller)
		{
			caller.begin=true;
			caller.pos.X=e.clientX-caller.handle.offset().left;
			caller.pos.Y=e.clientY-caller.handle.offset().top;
			var zIdx=parseInt(caller.drag.currentStyle().zIndex);
			if(JBF.zIndex>=zIdx)
			{
				JBF.zIndex ++;
				caller.drag.style.zIndex=JBF.zIndex;
			}
			else
			{
				//JBF.zIndex=zIdx+1;
				//caller.drag.style.zIndex=JBF.zIndex;
			}
			caller.fire(caller,'begin');
			
		},false,this);
		this.handle.on('blur',function(e,caller)
		{
			if(!caller.begin)
					return;
				caller.begin=false;
			caller.fire(caller,'end');
			
		},false,this);
		JBF(document).addEvents(
		{
			'mouseup':function(e,caller)
			{
				if(!caller.begin)
					return;
				caller.begin=false;
				caller.fire(caller,'end');
			},
			'mousemove':function(e,caller)
			{
				if(!caller.begin)
					return;
				window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
				
				caller.current.X=e.clientX-caller.pos.X;
				caller.current.Y=e.clientY-caller.pos.Y;
				
				caller.drag.style.left=caller.current.X+'px';
				caller.drag.style.top=caller.current.Y+'px';
				caller.fire(caller,'move');

			}
		},false,this);
		
	}					 
});

